<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\HotelReferral\StoreHotelReferralRequest;
use App\Http\Requests\HotelReferral\UpdateHotelRReferralRequest;
use App\Model\Gallery;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\Model\HotelReferral;
use App\Model\Category;
use DB;

class HotelReferralController extends Controller
{
    public function __construct()
    {
        parent::__construct();
        view()->share([
            '_hotel' => 'am-active',
        ]);
    }

    /**
     * 首页
     * @return mixed
     */
    public function index(Request $request)
    {
        $where = function ($query) use ($request) {

            if ($request->has('category_id') and $request->category_id != '-1') {
                $query->where('category_id', $request->category_id);
            }
        };

        $hotelreferrals = HotelReferral::with(['category','gallery'])
            ->orderBy('is_show', "desc")
            ->where($where)
            ->orderBy('sort_order')
            ->paginate(config('xSystem.page_size'));

        return view('Admin.xHotel.hotelreferral.index')->with('hotelreferrals', $hotelreferrals);

    }

    /**
     * 新增显示页
     */
    public function create()
    {
        $arr=array();
        $cate_id=HotelReferral::select('category_id')->get()->toArray();
        foreach ($cate_id as $v){
            $arr[]=$v['category_id'];
        }

        $this->attributes();
        return view('Admin.xHotel.hotelreferral.create')->with('cate_id',$arr);
    }

    /**
     * 新增
     * @param StoreHotelReferralRequest $request
     * equest
     */
    public function store(StoreHotelReferralRequest $request)
    {
        //return $request->all();
        $data = $request->except(['file', 'imgs']);
        $hotelReferral = HotelReferral::create($data);

        //相册
        if ($request->has('imgs')) {
            foreach ($request->imgs as $img) {
                $hotelReferral->gallery()->create(['img' => $img]);
            }
        }
        return redirect('/hotelreferral')->with('success', '添加成功');
    }

    private function attributes()
    {
        view()->share([
            'categories' => Category::get_categories(),
        ]);
    }
    /**
     * 修改显示页
     * @param $id
     */
    public function edit($id)
    {
        $arr=array();
        $hotelReferral = HotelReferral::with('gallery')
            ->find($id);

        $cate_id=HotelReferral::select('category_id')->get()->toArray();
        foreach ($cate_id as $v){
             $arr[]=$v['category_id'];
        }

        $this->attributes();
        return view('Admin.xHotel.hotelreferral.edit')->with('hotelReferral',$hotelReferral)->with('cate_id',$arr);
    }

    /**
     * 修改
     * @param UpdateHotelRReferralRequest $request
     * @param $id
     */
    public function update(UpdateHotelRReferralRequest $request, $id)
    {
        $data = $request->except(['file', 'imgs']);
        $hotelReferral = HotelReferral::find($id);
        $hotelReferral->update($data);

        //相册
        if ($request->has('imgs')) {
            foreach ($request->imgs as $img) {
                $hotelReferral->gallery()->create(['img' => $img]);
            }
        }
        return redirect('/hotelreferral')->with('success', '修改成功');
    }

    /**
     * 删除
     * @param $id
     */
    public function destroy($id)
    {

        if (!HotelReferral::check_children($id)) {
            Gallery::where('hotel_referral_id', $id)->delete();
        };
        
        HotelReferral::destroy($id);
        return back()->with('error','删除成功');
    }

    /**
     *是否
     * @param Request $request
     */
    public function is_something(Request $request)
    {

        $attr = $request->attr;
        $hotelreferral = HotelReferral::find($request->id);
        $value=$hotelreferral->$attr ? false : true;
        $hotelreferral->$attr=$value;
        $hotelreferral->save();

    }

    /**
     * ajax删除相册图片
     * @param Request $request
     */
    public function destroy_gallery(Request $request){
        Gallery::destroy($request->gallery_id);
    }


    public function destroy_checked(Request $request){
        $checked_id = $request->input("checked_id");
        HotelReferral::destroy($checked_id);
    }
    
    /**
     * 排序
     * @param Request $request
     */
    public function sort_order(Request $request)
    {
        $sort_order = $request->sort_order;
        $category = HotelReferral::find($request->id);
        $category->sort_order = $sort_order;
        $category->save();
    }

}
